﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace HaiFang
{
    [Serializable]
    public class ProxyUserOperateLog
    {
        private static string PROCEDURE_QueryUserOperateLog = "QueryUserOperateLog";
        private static string PROCEDURE_InsertUserOperateLog = "InsertUserOperateLog";
        //@CurrUserID		int = null,						--管理员编号
        //@StartTime 		datetime,						--开始时间
        //@EndTtime 		datetime,						--结束时间
        //@UserID			int = 0,						--用户编号
        //@Type			tinyint=0,						--1：登陆，2：添加，3：修改，4：删除，5：查询
        //@PageFirst	 	tinyint=0,						--是否第一次查询(默认0:首次查询)
        //@PageRow		int=1,							--页面显示数量
        //@PageJump		int=1,							--跳转页数	
        //@RecordNum		int output,
        //@PageNum		int output
        public static IList<UserOperateLog> QueryUserOperateLog(int CurrUserID, DateTime StartTime, DateTime EndTtime, int UserID, int Type, int PageFirst, int PageRow, int PageJump, out int RecordNum, out int PageNum)
        {
            IList<UserOperateLog> list = new List<UserOperateLog>();
            RecordNum = 0;
            PageNum = 0;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryUserOperateLog;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@StartTime", StartTime));
                    Parameters.Add(new SqlParameter("@EndTtime", EndTtime));
                    Parameters.Add(new SqlParameter("@UserID", UserID));
                    Parameters.Add(new SqlParameter("@Type", Type));
                    Parameters.Add(new SqlParameter("@PageFirst", PageFirst));
                    Parameters.Add(new SqlParameter("@PageRow", PageRow));
                    Parameters.Add(new SqlParameter("@PageJump", PageJump));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@RecordNum",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = RecordNum
                    });
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@PageNum",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = PageNum,
                         
                    });
                    #endregion

                    #region 获取执行结果
                    DataTable dt= helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<UserOperateLog>(dt);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@RecordNum")
                        {
                            RecordNum = helper.GetInt(sp.Value);
                        }
                        else if (sp.ParameterName == "@PageNum")
                        {
                            PageNum = helper.GetInt(sp.Value); 
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                RecordNum = 0;
                PageNum = 0;
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }
        //@GUID						varchar(128)='1',	 --GUID
        //@UserID 					bigint,
        //@Type						tinyint,			 --(1：登陆，2：添加，3：修改，4：删除，5：查询)
        //@Description				varchar(128)         --描述
        public static void InsertUserOperateLog(string GUID, int UserID, int Type, string Description)
        {
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertUserOperateLog;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@GUID", GUID)));
                    Parameters.Add(new SqlParameter("@UserID", UserID));
                    Parameters.Add(new SqlParameter("@Type", Type));
                    Parameters.Add(new SqlParameter("@Description", Description));
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class UserOperateLog
    {
        public int DisplayID { get; set; }
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public int UserID { get; set; }
        public int Type { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public string UserName { get; set; }
        public string TypeName { get; set; }
    }
}